# 订单服务发现和调用测试脚本
Write-Host "========================================" -ForegroundColor Green
Write-Host "订单服务发现和调用测试" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Green
Write-Host ""

# 测试函数
function Test-OrderServiceCall {
    param(
        [string]$Url,
        [string]$Description,
        [string]$Method = "GET",
        [string]$Body = $null
    )
    
    Write-Host "测试: $Description" -ForegroundColor Yellow
    Write-Host "URL: $Url" -ForegroundColor Gray
    
    try {
        $headers = @{"Content-Type" = "application/json"}
        
        if ($Method -eq "GET") {
            $response = Invoke-RestMethod -Uri $Url -Method GET -Headers $headers -TimeoutSec 10
        } else {
            $response = Invoke-RestMethod -Uri $Url -Method $Method -Body $Body -Headers $headers -TimeoutSec 10
        }
        
        Write-Host "[成功]" -ForegroundColor Green
        $response | ConvertTo-Json -Depth 3 | Write-Host -ForegroundColor Cyan
        return $true
    }
    catch {
        Write-Host "[失败] $($_.Exception.Message)" -ForegroundColor Red
        return $false
    }
    finally {
        Write-Host ""
    }
}

# 1. 检查订单服务是否运行
Write-Host "1. 检查订单服务状态..." -ForegroundColor Blue
try {
    $orderHealth = Invoke-RestMethod -Uri "http://localhost:5234/health" -TimeoutSec 5
    Write-Host "[成功] 订单服务正在运行" -ForegroundColor Green
}
catch {
    Write-Host "[错误] 订单服务未运行，请先启动订单服务" -ForegroundColor Red
    Write-Host "启动命令: dotnet run --project hao-cloud-api\hao-Code\4.Api\hao-Order.Api" -ForegroundColor Yellow
    Read-Host "Press Enter to exit"
    exit 1
}
Write-Host ""

# 2. 测试服务发现功能
Write-Host "2. 测试订单服务的服务发现功能..." -ForegroundColor Blue
$discoveryResult = Test-OrderServiceCall -Url "http://localhost:5234/api/order/test/services" -Description "服务发现测试"

# 3. 测试调用用户服务
Write-Host "3. 测试订单服务调用用户服务..." -ForegroundColor Blue
$testUserId = "00000000-0000-0000-0000-000000000001"
$userCallResult = Test-OrderServiceCall -Url "http://localhost:5234/api/order/test/user/$testUserId" -Description "调用用户服务测试"

# 4. 测试调用商品服务
Write-Host "4. 测试订单服务调用商品服务..." -ForegroundColor Blue
$testProductId = "00000000-0000-0000-0000-000000000001"
$productCallResult = Test-OrderServiceCall -Url "http://localhost:5234/api/order/test/product/$testProductId" -Description "调用商品服务测试"

# 5. 测试综合业务场景
Write-Host "5. 测试订单创建模拟场景..." -ForegroundColor Blue
$orderSimulationBody = @{
    userId = $testUserId
    productId = $testProductId
    productName = "测试商品"
    price = 100.00
    quantity = 1
} | ConvertTo-Json

$simulationResult = Test-OrderServiceCall -Url "http://localhost:5234/api/order/test/create-order-simulation" -Description "订单创建模拟测试" -Method "POST" -Body $orderSimulationBody

# 6. 测试结果汇总
Write-Host "========================================" -ForegroundColor Green
Write-Host "测试结果汇总" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Green

$results = @(
    @{Name="服务发现"; Result=$discoveryResult},
    @{Name="调用用户服务"; Result=$userCallResult},
    @{Name="调用商品服务"; Result=$productCallResult},
    @{Name="订单创建模拟"; Result=$simulationResult}
)

$passedTests = 0
$totalTests = $results.Count

foreach ($result in $results) {
    $status = if ($result.Result) { "✅ 通过" } else { "❌ 失败" }
    $color = if ($result.Result) { "Green" } else { "Red" }
    
    Write-Host "$($result.Name): $status" -ForegroundColor $color
    
    if ($result.Result) {
        $passedTests++
    }
}

Write-Host ""
Write-Host "总计: $passedTests/$totalTests 测试通过" -ForegroundColor $(if ($passedTests -eq $totalTests) { "Green" } else { "Red" })

if ($passedTests -eq $totalTests) {
    Write-Host ""
    Write-Host "🎉 所有测试通过！订单服务的服务发现功能正常：" -ForegroundColor Green
    Write-Host "✅ 服务发现机制工作正常" -ForegroundColor Green
    Write-Host "✅ 可以成功调用用户服务" -ForegroundColor Green
    Write-Host "✅ 可以成功调用商品服务" -ForegroundColor Green
    Write-Host "✅ 订单创建业务流程正常" -ForegroundColor Green
    Write-Host "✅ hao-Common.Http类库集成正常" -ForegroundColor Green
    Write-Host "✅ hao-Common.Consul服务发现正常" -ForegroundColor Green
} else {
    Write-Host ""
    Write-Host "⚠️  部分测试失败，请检查：" -ForegroundColor Yellow
    Write-Host "- 确保用户服务和商品服务都在运行" -ForegroundColor Yellow
    Write-Host "- 检查Consul服务注册状态" -ForegroundColor Yellow
    Write-Host "- 验证服务间网络连通性" -ForegroundColor Yellow
    Write-Host "- 查看订单服务日志排查问题" -ForegroundColor Yellow
}

Write-Host ""
Write-Host "📋 测试接口说明：" -ForegroundColor Cyan
Write-Host "- GET /api/order/test/services - 服务发现测试" -ForegroundColor Gray
Write-Host "- GET /api/order/test/user/{userId} - 调用用户服务" -ForegroundColor Gray
Write-Host "- GET /api/order/test/product/{productId} - 调用商品服务" -ForegroundColor Gray
Write-Host "- POST /api/order/test/create-order-simulation - 订单创建模拟" -ForegroundColor Gray

Write-Host ""
Read-Host "Press Enter to exit"